//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Commodity.h"
using namespace Cephei::QL::Experimental::Commodities;
#include <gen/QL/Experimental/Commodities/PricingError.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instrument.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Commodities::CCommodity::CCommodity (boost::shared_ptr<QuantLib::Commodity>& childNative, Object^ owner) : CInstrument(CCommodity::typeid)
{
#ifdef HANDLE
	_phCommodity = NULL;
#endif
	_ppCommodity = &childNative;
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCommodity));
}
Cephei::QL::Experimental::Commodities::CCommodity::CCommodity (QuantLib::Commodity& childNative, Object^ owner) : CInstrument(CCommodity::typeid)
{
#ifdef HANDLE
	_phCommodity = NULL;
#endif
	_ppCommodity = new boost::shared_ptr<QuantLib::Commodity> (&childNative);
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCommodity));
    _CommodityOwner = owner;
    _InstrumentOwner = owner;
}

Cephei::QL::Experimental::Commodities::CCommodity::CCommodity (CCommodity^ copy) : CInstrument(CCommodity::typeid)
{
#ifdef HANDLE
	_phCommodity = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCommodity = new boost::shared_ptr<QuantLib::Commodity> (copy->GetShared());
        _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCommodity));
    }
}
Cephei::QL::Experimental::Commodities::CCommodity::CCommodity (PLATFORM::Type^ t) : CInstrument(CCommodity::typeid)
{
#ifdef HANDLE
	_phCommodity = NULL;
#endif
	if (!t->IsSubclassOf(CCommodity::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Commodities::CCommodity::CCommodity (QuantLib::Handle<QuantLib::Commodity>& childNative, Object^ owner)  : CInstrument(CCommodity::typeid)
{
	_phCommodity = &childNative;
	_ppCommodity = &static_cast<boost::shared_ptr<QuantLib::Commodity>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCommodity));
    _CommodityOwner = owner;
}
Cephei::QL::Experimental::Commodities::CCommodity::CCommodity (QuantLib::Handle<QuantLib::Commodity> childNative)  : CInstrument(CCommodity::typeid)
{
	_phCommodity = &childNative;
	_ppCommodity = &static_cast<boost::shared_ptr<QuantLib::Commodity>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCommodity));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Commodities::CCommodity::CCommodity (QuantLib::Commodity childNative)  : CInstrument(CCommodity::typeid)
{
#ifdef HANDLE
	_phCommodity = NULL;
#endif
	_ppCommodity = new boost::shared_ptr<QuantLib::Commodity> (new QuantLib::Commodity (childNative));
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCommodity));
}
#endif

Cephei::QL::Experimental::Commodities::CCommodity::~CCommodity ()
{
    if (_ppCommodity != NULL)
    {
	    delete _ppCommodity;
        _ppCommodity = NULL;
    }
}
Cephei::QL::Experimental::Commodities::CCommodity::!CCommodity ()
{
    if (_ppCommodity != NULL)
    {
	    delete _ppCommodity;
    }
}
QuantLib::Commodity& Cephei::QL::Experimental::Commodities::CCommodity::GetReference ()
{
    if (_ppCommodity == NULL) throw REFNEW NativeNullException ();
	return **_ppCommodity;
}
boost::shared_ptr<QuantLib::Commodity>& Cephei::QL::Experimental::Commodities::CCommodity::GetShared ()
{
    if (_ppCommodity == NULL) throw REFNEW NativeNullException ();
	return *_ppCommodity;
}
QuantLib::Commodity* Cephei::QL::Experimental::Commodities::CCommodity::GetPointer ()
{
    if (_ppCommodity == NULL) throw REFNEW NativeNullException ();
	return &**_ppCommodity;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Commodity>& Cephei::QL::Experimental::Commodities::CCommodity::GetHandle ()
{
	if (_phCommodity == NULL)
	{
		_phCommodity = new Handle<QuantLib::Commodity> (*_ppCommodity);
	}
	return *_phCommodity;
}
#endif
bool Cephei::QL::Experimental::Commodities::CCommodity::HasNative () 
{
	return (_ppCommodity != NULL);
}

Cephei::QL::Experimental::Commodities::ICommodity^ Cephei::QL::Experimental::Commodities::CCommodity::AddPricingError (QL::Experimental::Commodities::PricingError::LevelEnum errorLevel, String^ error, Microsoft::FSharp::Core::FSharpOption<String^>^ detail)
{
    try
    {
        QuantLib::PricingError::Level _errorLevel = (QuantLib::PricingError::Level)errorLevel ;
        std::string _error = (std::string)ValueHelper::Convert (error); //a
        std::string _detail = 
            (Microsoft::FSharp::Core::FSharpOption<String^>::IsSome::get (detail) ? (std::string)ValueHelper::Convert (detail->Value) : ""); //9a
    	(*_ppCommodity)->addPricingError ( _errorLevel,  _error,  _detail );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::Experimental::Commodities::IPricingError^>^ Cephei::QL::Experimental::Commodities::CCommodity::PricingErrors::get ()
{
    try
    {
    	std::vector<QuantLib::PricingError>& _rv = (std::vector<QuantLib::PricingError>&)(*_ppCommodity)->pricingErrors ( );   
        CoVector<Cephei::QL::Experimental::Commodities::IPricingError^>^ _nrv = REFNEW CoVector<Cephei::QL::Experimental::Commodities::IPricingError^>(REFNEW CPricingErrorVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

